Calculator = Ext.extend(Ext.app.Module, {
    id: 'calculator-win',
	data: new Ext.form.TextField({
		emptyText: '0',
		style: 'text-align: right'
	}),
	
	init: function(){
		this.launcher = {
			text: 'Calculator',
			iconCls: 'icon-grid',
			handler: this.createWindow,
			scope: this
		}
    },

    createWindow: function() {
		var self = this;
		var desktop = this.app.getDesktop();
		var win = desktop.getWindow('calculator-win');
		var panel1 = new Ext.Panel({
			layout: 'fit',
			baseCls: 'x-plain',
			items: this.data,
			style: 'padding: 4px'
		});
		
		var waitingForOperation = 0;
		var firstOperand;
		var secondOperand;
		var operation;
		
		function setData(data) {
			if(!waitingForOperation) {
				var val = self.data.getValue();
				if((data.text === '.') && (val.search(/\./i) != -1)) return;
				self.data.setValue(self.data.getValue() + data.text);
			}
			else {
				self.data.setValue(data.text);
				waitingForOperation = 0;
			}
		}
		
		function setOperation(data) {
			waitingForOperation = 1;
			firstOperand = self.data.getValue();
			operation = data.text;
		}
		
		function doOperation(data) {
			secondOperand = self.data.getValue();
			switch(operation) {
				case '+':
					self.data.setValue(parseFloat(firstOperand) + parseFloat(secondOperand));
					break;
				case '-':
					self.data.setValue(parseFloat(firstOperand) - parseFloat(secondOperand));
					break;
				case '*':
					self.data.setValue(parseFloat(firstOperand) * parseFloat(secondOperand));
					break;
				case '/':
					self.data.setValue(parseFloat(firstOperand) / parseFloat(secondOperand));
					break;
			}
		}

		var panel2 = new Ext.Panel({
			layout: 'table',
			frame: true,
			defaults: {
				bodyStyle: 'padding: 4px'
			},
			layoutConfig: { columns: 4 },
			items: [
				{ items: { xtype: 'button', minWidth: '80', text: 'CE', handler: function() { self.data.setValue('') } }, colspan: 2 },
				{ items: { xtype: 'button', minWidth: '80', text: 'C', handler: function() { self.data.setValue('') } }, colspan: 2 },
				{ items: { xtype: 'button', text: '7', minWidth: '35', handler: setData } },
				{ items: { xtype: 'button', text: '8', minWidth: '35', handler: setData } },
				{ items: { xtype: 'button', text: '9', minWidth: '35', handler: setData } },
				{ items: { xtype: 'button', text: '/', minWidth: '35', handler: setOperation } },
				{ items: { xtype: 'button', text: '4', minWidth: '35', handler: setData } },
				{ items: { xtype: 'button', text: '5', minWidth: '35', handler: setData } },
				{ items: { xtype: 'button', text: '6', minWidth: '35', handler: setData } },
				{ items: { xtype: 'button', text: '*', minWidth: '35', handler: setOperation } },
				{ items: { xtype: 'button', text: '1', minWidth: '35', handler: setData } },
				{ items: { xtype: 'button', text: '2', minWidth: '35', handler: setData } },
				{ items: { xtype: 'button', text: '3', minWidth: '35', handler: setData } },
				{ items: { xtype: 'button', text: '-', minWidth: '35', handler: setOperation } },
				{ items: { xtype: 'button', text: '0', minWidth: '35', handler: setData } },
				{ items: { xtype: 'button', text: '.', minWidth: '35', handler: setData } },
				{ items: { xtype: 'button', text: '=', minWidth: '35', handler: doOperation } },
				{ items: { xtype: 'button', text: '+', minWidth: '35', handler: setOperation } }
			]
		});
		if(!win) {
			win = desktop.createWindow({
				id: 'calculator-win',
				title: 'Calculator',
				width: 200,
				height: 218,
				resizable: false,
				maximizable: false,
				items: [ panel1, panel2 ]
			});
		}
		win.show();
		win.on('close', function() {
			self.data.setValue('0');
		});
    },
});

